package com.yc.jpyy.profession.controller;


import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.profession.service.ProfessionService;
import com.yc.jpyy.utils.Tools;


@Controller
@RequestMapping(value = "/profession")
@Scope("prototype")
public class ProfessionController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(ProfessionController.class);

	@Autowired
	private ProfessionService professionService;
	
	
	/**
	 * 新增
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addProfession", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String addProfession(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String checkParameter= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(checkParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = professionService.addProfession(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("addProfession" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "添加失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 修改
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateProfession", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateProfession(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = professionService.updateProfession(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("updateProfession" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "修改失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 查询
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/selectProfession", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String selectProfession(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String checkParameter= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(checkParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = professionService.selectProfession(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("selectProfession" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/selectProfessionByPages", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String selectProfessionByPages(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String checkParameter= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(checkParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = professionService.selectProfessionByPages(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("selectProfessionByPages" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	
	/**
	 * 删除
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/delProfession", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String delProfession(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String checkParameter= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(checkParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = professionService.delProfession(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("delProfession" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "删除失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 行业下拉框
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getProfessionInfo", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String getProfessionInfo(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String checkParameter= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(checkParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = professionService.getProfessionInfo(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("getProfessionInfo" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "获取失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 根据行业查询所有科目信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getProfessionSubject", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String getProfessionSubject(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String checkParameter= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(checkParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = professionService.getProfessionSubject(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("getProfessionSubject" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "获取失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	
		@SuppressWarnings("finally")
		@RequestMapping(value = "/IsOnProfessionName", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String IsOnProfessionName(HttpServletRequest request) throws UnsupportedEncodingException {
			
			RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			
			
			try{
				String info = request.getParameter("info");
				System.out.println("info"+info);
				Map<String,Object> paramsMap = Tools.JsonToMap(info);
				String dbcode = paramsMap.get("dbcode").toString();
				
				//设置数据源
				HandlerDataSource.setDataSource(dbcode);
				int ret = professionService.IsOnProfessionNameService(paramsMap);
				response.setReturnMsg("成功");
				response.setListData(ret);
				
			} catch (Exception e) {
				logger.error("异常" + e.toString());
				// 异常信息
				response.setReturnCode(0);
				response.setReturnMsg("操作失败");
				response.setReturnException(e.toString());
				e.printStackTrace();
			}
			
			finally {
				
				HandlerDataSource.clear();
				return response.toJSON();
			}
			
		}
		
		@SuppressWarnings("finally")
		@RequestMapping(value = "/IsOnSubjectName", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String IsOnSubjectName(HttpServletRequest request) throws UnsupportedEncodingException {
			
			RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			
			
			try{
				String info = request.getParameter("info");
				System.out.println("info"+info);
				Map<String,Object> paramsMap = Tools.JsonToMap(info);
				String dbcode = paramsMap.get("dbcode").toString();
				
				//设置数据源
				HandlerDataSource.setDataSource(dbcode);
				int ret = professionService.IsOnSubjectNameService(paramsMap);
				response.setReturnMsg("成功");
				response.setListData(ret);
				
			} catch (Exception e) {
				logger.error("异常" + e.toString());
				// 异常信息
				response.setReturnCode(0);
				response.setReturnMsg("操作失败");
				response.setReturnException(e.toString());
				e.printStackTrace();
			}
			
			finally {
				HandlerDataSource.clear();
				return response.toJSON();
			}
			
		}
}
